var ajaxGet = function (request) {
    var r = new XMLHttpRequest()
    r.open('GET', request.url, true)
    r.onreadystatechange = function () {
        if (r.readyState === 4) {
            request.callback(r.response)
        }
    }
    r.send()
}

var ajax = function (ku) {
    var r = new XMLHttpRequest()
    r.open(ku.method, ku.url, true)
    r.setRequestHeader('Content-Type', ku.contentType)
    r.onreadystatechange = function () {
        if (r.readyState === 4) {
            ku.callback(r.response)
        }
    }
    r.send(ku.data)
}

var allTodos = function () {
    var request = {
        url: 'http://152.136.245.180/todo/8190310/all',
        callback: function (response) {
            console.log(response)
        }
    }
    ajaxGet(request)
}

/*
2.POST
{"task":"study"}
http://152.136.245.180/todo/<你的qq号>/add
* */
var addTodo = function () {
    var request = {
        url: 'http://152.136.245.180/todo/8190310/add',
        method: 'POST',
        contentType: 'application/json',
        data: '{"task":"study"}',
        callback: function (response) {
            console.log('add', response)
        }
    }
    ajax(request)
}
/*
3.发送 JSON 格式字符串 来更新一个todo
要求设置 Content-Type 为 application/json
POST
{"task":"study"}
http://152.136.245.180/todo/<你的qq号>/update/<todo_id>
4.删除一个todo
GET
http://152.136.245.180/todo/<你的qq号>/delete/<todo_id>
* */

var updateTodo = function () {
    var request = {
        url: 'http://152.136.245.180/todo/8190310/update/002',
        method: 'POST',
        contentType: 'application/json',
        data: '{"task":"paly games"}',
        callback: function (response) {
            console.log('update', response)
        }
    }
    ajax(request)
}
//回调函数
var deleteTodo = function (todoId, complete) {
    var request = {
        url: ' http://152.135.245.180/todo/8190310/delete/' + todoId,
        callback: function (response) {
            console.log('delete, ', response)
            var t = JSON.parse(response)
            complete(t)
        }
    }
    ajaxGet(request)
}

deleteTodo(123, function (todo) {
    console.log('todo complete, ', todo)
})

//==================面向对象编程思想，重构代码：面向对象：4个api
//好处：重构后，修改url很方便
//get post 是ajax；add all delete update都不是ajax
var TodoApi = function () {
    this.url = 'http:152.135.245.180/todo/'
    this.qq = '819031'
}

TodoApi.prototype.get = function (path, callback) {
    var url = `${this.url}/${this.qq}` + path
    var r = new XMLHttpRequest()
    r.open('GET', url, true)
    r.onreadystatechange = function () {
        if (r.readyState === 4) {
            callback(r.response)
        }
    }
    r.send()
}

TodoApi.prototype.post = function (path, data, callback) {
    var url = `${this.url}/${this.qq}` + path
    var r = new XMLHttpRequest()
    r.open('POST', url, true)
    r.setRequestHeader('Content-Type', 'application/json')
    r.onreadystatechange = function () {
        if (r.readyState === 4) {
            callback(r.response)
        }
    }
    var d = JSON.stringify(data)
    r.send(d)
}

TodoApi.prototype.all = function (callback) {
    var path = '/all'
    this.get(path, callback)
}

TodoApi.prototype.add = function (todo, callback) {
    var path = '/add'
    this.post(path, todo, callback)
}

TodoApi.prototype.update = function (todo, callback) {
    var path = '/update/' + todo.id
    this.post(path, todo, callback)
}

TodoApi.prototype.delete = function (todoId, callback) {
    var path = '/delete/' + todoId
    this.get(path, callback)
}

/*
todo 后端程序提供4个API
1.获得所有 todo,返回的是一个数组
GET
http:152.135.245.180/todo/<你的qq号>/all
2.发送 JSON格式字符串来创建一个 todo
要求设置Content-Type为application/json
===========================================
POST
{"task":"study"}
http:152.135.245.180/todo/<你的qq号>/add

3.发送JSON格式字符串来更新一个todo
要求设置Content-Type为application/json

POST
{"task":"study"}
http:152.135.245.180/todo/<你的qq号>/update/<todo_id>

4.删除一个 todo
GET
http:152.135.245.180/todo/<你的qq号>/delete/<todo_id>
 */